<?php
include('header.php');
$delete = make_safe($_GET['delete']);
if($delete !== NULL){
	$delete_student = "delete from students where id='$delete'";
	if (!mysql_query($delete_student)){
	  	die('Error: ' . mysql_error());
	}
	//header('Location: index.php');
	//this doesn't work because header.php is included
	//you can't have any html, etc data before header()'s
}
$manage = "current_page_item";
include('nav.php');
?>

<div class="primary">
<br/>
<?php
$user = get_user();
if($user !== NULL){ ?>
	<div class="add_contact">
	<form action="form.php" method="post">
		<label for="student_name">Name:</label> <input name="student_name" type="text" size="20"/><br/>
   		<label for="lastname">Family Name:</label> <input name="student_lastname" type="text" size="20"/><br/>
		<label for="birthday">Birthday:</label> <input name="student_birthday" type="text" size="20"/><br/>        
		<label for="street">Street:</label> <input name="student_street" type="text" size="20"/><br/>
		<label for="town">City:</label> <input name="student_town" type="text" size="20"/><br/>
		<label for="state">State:</label>
		<select name="student_state">
		    <option value="PZ">Prizren</option>
			<option value="PR">Prishtina</option>
			<option value="PJ">Peja</option>
		</select><br/>
		<label for="zip">Zip Code:</label> <input name="student_zip" type="text" size="5" maxlength="5"/><br/>
		<input type="hidden" name="action" value="add_student"/><br/>
		<input type="submit" id="submit" value="Add student"/>
	</form><br/>
	<?php
	$num_students = "select count(*) from students where created_by='$user'";
    $result = mysql_query($num_students);
    $array = mysql_fetch_array($result);
    //the long way of counting # of items in a database table
	?>
	<p>You are currently managing <?php echo $array[0]; ?> students.</p>
	</div>
    
    <div class="note">Note: this is a note</div>
    <div class="warn"><strong>Warning</strong>: you have been warned!</div>    
    	
	<?php
	echo "<div class='contact_list'>";
	$get_contacts = "select * from contacts where created_by='$user' order by name, state";
    $result = mysql_query($get_contacts);
    //loop! loops through the multiple results of the $get_contacts query
    while($student = mysql_fetch_array($result, MYSQL_ASSOC)){
		echo "<b>" . $student['name'] . "</b><br/>" . $student['street'] . "<br/>";
		echo $student['city'] . ", " . $student['state'] . " " . $student['zip'];
		echo "<br/><a href='manage.php?delete=" . $student['id'] . "'><small>[Delete]</small></a><br/><br/>";
	}
	echo "</div>";
 } else { ?>
	<p>Please login to use this feature.</p>
<?php } ?>
  
</div>
<?php include('footer.php'); ?>